<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Model\CustBankCard;
use Illuminate\Http\Request;
use App\Repositories\AccountRepository;

class AccountController extends Controller
{
    private $account = null;

    public function __construct(AccountRepository $account)
    {
        $this->middleware(["auth:api", "App\Http\Middleware\UserForbidden"]);

        $this->account = $account;
    }

    /**
     * 提现
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function withdraw(Request $request)
    {
        $user = $request->user();

        $validator = \Validator::make($request->all(), [
            'cash_amount' => 'required|numeric|min:2',
            'bankcard_id' => 'required|numeric|min:1',
            'withdraw_pw' => 'required|between:6,20'
        ], [
            'cash_amount.required' => "提款金额不能为空",
            'bankcard_id.required' => "请选择提现银行卡",
            'withdraw_pw.required' => "提款密码不能为空",
            'cash_amount.numeric' => "请填写正确的提款金额",
            'cash_amount.min' => '提款金额不得少于2元',//根据需求设置
            'bankcard_id.min' => '请选择提现的银行卡',
            'withdraw_pw.between' => '提款密码格式不合法',
        ]);

        if ($validator->fails()) {
            return parent::jsonReturn([], parent::CODE_FAIL, $validator->errors()->first());
        }

        if ($user->withdraw_pw != encryptPassword($request->get('withdraw_pw'))) {
            return parent::jsonReturn([], parent::CODE_FAIL, '提现密码错误');
        }

        if ($user->is_cash_forbidden) {
            return parent::jsonReturn([], parent::CODE_FAIL, '用户暂时无法提现');
        }

        if ($request->get('cash_amount') > $user->cust_capital_amount) {
            return parent::jsonReturn([], parent::CODE_FAIL, '提款金额不能大于用户余额');
        }

        if (!$user->real_name || !$user->id_card) {
            return parent::jsonReturn([], parent::CODE_FAIL, '请先实名认证才能提现');
        }

        $ret = $this->account->withDraw($user, $request->only(['cash_amount', 'bankcard_id',
            'withdraw_pw', 'cust_remark']));
        return $ret ? parent::jsonReturn([], parent::CODE_SUCCESS, "提交成功") :
            parent::jsonReturn([], parent::CODE_FAIL, '提交失败');
    }

    /**
     * 提现记录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function withdrawRecord(Request $request)
    {
        $ret = $this->account->withdrawRecord($request->user());
        return $ret ? parent::jsonReturn($ret, parent::CODE_SUCCESS, "success") :
            parent::jsonReturn([], parent::CODE_FAIL, "查询失败");
    }

    /**
     * 撤回提现
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function checkBackWithdraw(Request $request)
    {
        $user = $request->user();
        $validator = \Validator::make($request->all(), [
            "id" => "required|min:1|integer"
        ], [
            "id.require" => "id错误",
            "id.min" => "id错误",
        ]);

        if ($validator->fails()) {
            return parent::jsonReturn([], parent::CODE_FAIL, $validator->errors()->first());
        }

        $ret = $this->account->checkBackWithdraw($user, $request->get("id"));
        return $ret ? parent::jsonReturn([], parent::CODE_SUCCESS, "撤销成功") :
            parent::jsonReturn([], parent::CODE_FAIL, '撤销失败');
    }

    /**
     * 获取账户数据统计
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCount(Request $request)
    {
        $user = $request->user();
        $ret = $this->account->getCount($user);
        return $ret ? parent::jsonReturn($ret, parent::CODE_SUCCESS, "获取成功") :
            parent::jsonReturn([], parent::CODE_FAIL, '获取失败');
    }
}